package com.testdemo.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.labmanager.JsonModule.RequestBase;
import com.labmanager.JsonModule.ResponseBase;
import com.testdemo.web.service.GroupService;
import com.testdemo.web.service.LabService;
import com.testdemo.web.service.Manager;
import com.testdemo.web.service.MyUserService;
import com.testdemo.web.vo.Group;
import com.testdemo.web.vo.MyUser;
import com.testdemo.web.vo.Session;
import com.testdemo.web.vo.SessionManager;
import com.testdemo.web.vo.UserInfo;

@Controller
@RequestMapping("/manager")
public class ManagerController {
	
	private SessionManager sessionManager;
	
	public ManagerController() {
		sessionManager = AccountController.sessionManager;
	}
	
	@Resource
	private Manager manager;
	@Resource
	private MyUserService myUserService;
	@Resource
	private LabService labService;
	@Resource
	private GroupService groupService;
	
	@RequestMapping(value = "/get", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase GetUserInLabOrGroup(@RequestBody Group group) {
		ResponseBase responseBase = new ResponseBase();
		//检查数据
		if(group.checkData() == false) {
			responseBase.setError(-5, "缺少必须参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(group.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		//权限检验
		UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
		if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
			responseBase.setError(-1, "你没有权限查看成员信息");
			return responseBase;
		}
		group.setLabid(userInfo.getLabid());
		List<UserInfo>list = manager.getAllUserInfoInLabOrGroup(group.getLabid(), group.getGroupid());
		responseBase.setData(list);
		return responseBase;
	}
	
	@RequestMapping(value = "/getapply", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase GetApplyList(@RequestBody RequestBase requestBase) {
		ResponseBase responseBase = new ResponseBase();
		//数据检验
		if(requestBase.checkData() == false) {
			responseBase.setError(-5, "缺少必选参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(requestBase.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		//权限检验
		UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
		if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
			responseBase.setError(-1, "你没有权限查看申请");
			return responseBase;
		}		
		List<MyUser>list = manager.getApplyInLabByLabid(userInfo.getLabid());
		responseBase.setData(list);
		return responseBase;
		
	}
	
	@RequestMapping(value = "/check", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase CheckApplyInLab(@RequestBody MyUser user) {
		ResponseBase responseBase = new ResponseBase();
		//数据检验
		if(user.checkData() == false) {
			responseBase.setError(-5, "缺少必选参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(user.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		//权限检验
		UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
		if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
			responseBase.setError(-1, "你没有权限审核申请");
			return responseBase;
		}		
		manager.checkApplyInLab(user);
		return responseBase;
	}
	
	
	@RequestMapping(value = "/devide", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase DevideGroup(@RequestBody Group group) {
		ResponseBase responseBase = new ResponseBase();
		//数据检验
		if(group.checkData() == false) {
			responseBase.setError(-5, "缺少必选参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(group.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		//权限检验
		UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
		if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
			responseBase.setError(-1, "你没有权限划分小组");
			return responseBase;
		}			
		manager.devideUserInToGroup(group);
		
		return responseBase;
	}
	
	
	@RequestMapping(value = "/chargeman", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase SetChargeMan(@RequestBody Group group) {
		ResponseBase responseBase = new ResponseBase();
		//数据检验
		if(group.checkData() == false) {
			responseBase.setError(-5, "缺少必选参数");
			return responseBase;
		}
		//登陆检验
		Session session = sessionManager.selectSessionByAccessToken(group.getSession());
		if(session == null) {
			responseBase.setError(-1, "你还没有登陆，请先登陆");
			return responseBase;
		}
		//权限检验
		UserInfo userInfo = myUserService.selectUserInfoByName(session.getUsername());
		if(!labService.checkuserName(userInfo.getUsername(), userInfo.getLabid())) {
			responseBase.setError(-1, "你没有权限设置组长");
			return responseBase;
		}			
		Group orginGroup = groupService.selectGroupByGroupId(group.getGroupid());
		group.setGroupname(orginGroup.getGroupname());
		group.setLabid(orginGroup.getLabid());
		groupService.updateGroup(group);
		manager.devideUserInToGroup(group);
		
		return responseBase;
	}
}
